const callbacks = new WeakMap();

let isLoad = false;

const resizeObserver = new ResizeObserver((entries) => {
  if (isLoad === false) {
    isLoad = true;
  } else {
    entries.forEach((entry) => {
      const callback = callbacks.get(entry.target);
      if (callback) {
        callback?.(entry);
      }
    });
  }
});

export const vResize = {
  mounted(el, binding) {
    if (binding.value) {
      callbacks.set(el, binding.value);
      resizeObserver.observe(el);
    }
  },
  unmounted(el) {
    callbacks.delete(el);
    resizeObserver.unobserve(el);
  }
};
